package it.softphone.cmc.server.service.json;



import it.softphone.commons.json.server.JSONConverter;
import it.softphone.commons.json.server.JSONObject;


public class JSONAutoBeanDateConverter implements JSONConverter<java.util.Date> {

	public Object convert(java.util.Date value) {
		
		return (value == null ) ? JSONObject.NULL : Long.toString(value.getTime());

	}

	@SuppressWarnings("rawtypes")
	public Object convert(Class type, Object value) {
		if( JSONObject.NULL==value || null==value ) return null;
	
		if( value instanceof String ) {
			return new java.util.Date( Long.parseLong((String)value) );
		}
		if( value instanceof Long ) {
			return new java.util.Date( (Long)value );
		}
		if( value.getClass().isInstance(Long.TYPE)) {
			return new java.util.Date( (Long)value );
		}
		
		return value;
	}
	
}